<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/1/17
 * Time: 16:09
 */
class DbMysql extends Db{

    public function connect(){
        $link = @mysql_connect(C('DB_HOST'),C('DB_USER'),C('DB_PASSWORD'));
        //设置字符集
        mysql_query('SET NAMES '.C('DB_CHARSET'));
        //选择数据库
        mysql_select_db(C('DB_DATABASE'));
        if(!$link){
            halt(mysql_error());
        }
    }

    public function exe($sql){
        if(!mysql_query($sql)){
            echo mysql_error();
        }
        //新增存在id返回自己，否则返回响应条数
        return mysql_insert_id()?mysql_insert_id():mysql_affected_rows();
    }

    public function query($sql){
        $result = mysql_query($sql);
        if($result){
            $rows = [];
            while($row = mysql_fetch_assoc($result)){
                $rows[] = $row;
            }
            return $rows;
        }else{
            $this->error = mysql_error();
            return false;
        }
    }

}